import java.io.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 14394 on 2017/12/29.
 */
public class Demo {


    public static void main(String[] args) {
        String WorkUrl = "";
        List<Student> students= new LinkedList<>();
        Date date = new Date();
        System.out.println("============"+date.toString()+"=====================");
        System.out.println("                     欢迎使用作业查漏系统    -----DESIGNED BY 槑槑冏");
        System.out.println("请输入您的命令：");
        System.out.println("     1.指定班级学生名单");
        System.out.println("     2.指定作业文件夹");
        System.out.println("     3.作业查漏");
        System.out.println("     4.用户指南");
        System.out.println("     5.退出");
        while (true){

        int m= Integer.parseInt(new Scanner(System.in).next());
        switch (m){
            case 5:
                System.out.println("欢迎下次使用！");
                return;
            case 4:
                System.out.println("用户指定学生名单(仅支持txt格式)-->指定提交作业所在的文件夹-->查漏");
            case 3:
                check(students,WorkUrl);
                break;
            case 2:
                System.out.println("请指定学生文件所在的路径。");
                String s= new Scanner(System.in).next();
                if (s!=null&&!s.equals(" ")){
                    WorkUrl = s;
                    System.out.println("指定成功。。。");
                }
                break;

            case 1:
                System.out.println("请指定学生名单所在的路径。");
                String s1= new Scanner(System.in).next();
                students= setStudentList(s1);
                System.out.println("指定成功");
                break;

            default:
                System.out.println("请输入正确的指令！");
        }
            System.out.println("请输入您的命令：");
        }
    }


    public  static List<Student> setStudentList(String url){

        List<Student> ss=new LinkedList<>();
        try {
            BufferedReader reader= new BufferedReader(new FileReader(url));
            String temp = null;
            while ((temp= reader.readLine())!=null){
                   String[] temp1= temp.split("\\s");
                    Student student= new Student();
                    student.setSno(Integer.parseInt(temp1[0]));
                    student.setName(temp1[1]);
                    ss.add(student);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ss;
    }

    public static void check(List<Student> s1,String workUrl){
        int size=0;
        if (s1.size()==0||workUrl.equals("")){
            System.out.println("请先指定你班学生的名单以及作业的文件夹，详情请看用户指南！");
        }else {
            File file= new File(workUrl);
            String[] workNames= file.list();
            for (int i=0;i<workNames.length;i++){
                Pattern p = Pattern.compile("(\\d{10})");
            Matcher m= p.matcher(workNames[i]);
            String tt="";
            if (!m.find()) System.out.println("学号为 "+m.group(1)+" 有错误。");
            Matcher m1= p.matcher(workNames[i]);
            while (m1.find()){
                tt=m1.group(1);
            }
            for (int j=0; j<s1.size();j++){
                if (s1.get(j).getSno()==Integer.parseInt(tt)){
                    s1.get(j).setFlag(true);
                    size++;
                }
            }
            }
                System.out.println("班级总人数:"+s1.size()+";已交:"+size+"人;");
                if (s1.size()>size){
                    System.out.println("还有" + (s1.size()-size)+"人未交。 分别是:");
                    for (Student s:s1){
                        if (!s.isFlag()){
                            System.out.println("学号："+s.getSno()+";  姓名："+s.getName());
                        }
                    }
                }
        }

    }




}
